package com.demo.juc.futuretask;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @Author: shubin_yao
 * @Date: 2022/09/25/9:31
 * @Description:  CompleteFutrueTask 是FutureTask扩展，类似监听，观察者模式
 *
 * 如何用 4个静态方法 创建一个CompleteFutrueTask
 *
 */
public class CompleteFutrueTaskThread {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ExecutorService threadPool = Executors.newFixedThreadPool(3);

        //默认线程名，返回null
        /*CompletableFuture<Void> completableFuture = CompletableFuture.runAsync(() -> {
            System.out.println(Thread.currentThread().getName());
        });*/


        //自定义线程，返回null
       /* CompletableFuture<Void> completableFuture = CompletableFuture.runAsync(() -> {
            System.out.println(Thread.currentThread().getName());
        },threadPool);*/


        //默认线程名，返回T
        /*CompletableFuture<String> completableFuture = CompletableFuture.supplyAsync(() -> {
            System.out.println(Thread.currentThread().getName());
            return "hello supplyAsync";
        });*/


        //自定义线程，返回T
        CompletableFuture<String> completableFuture = CompletableFuture.supplyAsync(() -> {
            System.out.println(Thread.currentThread().getName());
            return "hello supplyAsync";
        },threadPool);

        System.out.println(completableFuture.get());


    }

}
